﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Problem3
{
	// Find the largest prime factor of a composite number.
	class Problem0003
	{
		static void Main(string[] args)
		{
			var list = PrimeFactors(600851475143);

			Console.WriteLine(list.Last());
			Console.WriteLine(list.First());
			Console.Read();
		}

		/// <summary>
		/// Returns a list of the prime factors of a number in ascending order.
		/// </summary>
		/// <param name="i">The number to factor</param>
		/// <returns>List<int> of prime factors</int></returns>
		/// <example>Passing 36 to this method returns a list containing 2, 2, 3, 3.</example>
		/// <exception cref="ArgumentOutOfRangeException">Number to factor must be 2 or greater</exception>
		public static List<long> PrimeFactors(long i)
		{
			if (i < 2)
			{
				throw new ArgumentOutOfRangeException("Numbers less than 2 don't have prime factors");
			}

			List<long> result = new List<long>();

			int divisor = 2;

			while (divisor <= i)
			{
				if (i % divisor == 0)
				{
					result.Add(divisor);
					i /= divisor;
				}
				else
				{
					divisor++;
				}
			}

			return result;
		}
	}
}
